import httpUtil from "@/utils/httpUtil.js"
import userApi from "./userApi"
import deviceUtil from "@/utils/deviceUtil.js"

const initData = (option) => {
	let params = {
			v1:1,
			v2:101,
			text:JSON.stringify(option)
		}
		httpUtil.post('/api/rf/addLog', params, res => {
			
		})
	if (option.a) {
		httpUtil.setA(option.a)
	}
	if(deviceUtil.isWeChatBrowser()){
		if (option.code) {
		
			uni.setStorageSync('code', option.code)
			
				// alert(uni.getStorageSync('code'))
		}else{
			uni.setStorageSync('local', location.href)
			PumpkinPay.authorization({
			 url: uni.getStorageSync('local'),
			 })
		}
	}

	if (option.openid) {
		userApi.setUserId(option.openid)
	}
	if (option.qcjParamStr) {
		setParam(option.qcjParamStr)
	}
	if (option.param) {
		setParam(option.param)
	}
	if (option.jtcAdRequestId) {
		setParam(option.jtcAdRequestId)
	}
	if (option.push_id) {
		setParam(option.push_id)
		setPushId(option.push_id)
	}
	if (option.pushid) {
		setParam(option.pushid)
		setPushId(option.pushid)
	}
	if (option.token) {
		setParamToken(option.token)
	}

	if (option.clickid) {
		setParam(option.clickid)
	}
	if (option.tokenid) {
		setParam(option.tokenid)
	}
	if (option.tokenId) {
		setParam(option.tokenId)
	}

	
	if (option.adId) {
		setParam(option.adId)
	}
	if (option.bxm_id) {
		setParam(option.bxm_id)
	}
	if (option.a_oId) {
		setParam(option.a_oId)
	}
	if (option.a_oid) {
		setParam(option.a_oid)
	}
	if (option.cpparamOrderId) {
		setParam(option.cpparamOrderId)
	}
	
	if (option.tid) {
		
		let a={
			'tid':option.tid,
			'lbid':option.lbid,
			
		}
		if(a.tid==null||a.tid==undefined||a.tid=='null'){
			a.requestId=''
		}
		if(a.lbid==null||a.lbid==undefined||a.lbid=='null'){
			a.lbid=''
		}
		setParam(JSON.stringify(a))
		//oppo
	}
	
	if (option.imei) {
		
		let a={
			'imei':option.imei,
			'adId':option.adId,
			
		}
		if(a.imei==null||a.imei==undefined||a.imei=='null'){
			a.imei=''
		}
		if(a.adId==null||a.adId==undefined||a.adId=='null'){
			a.adId=''
		}
		setParam(JSON.stringify(a))
		//oppo
	}
	// if (option.lbid) {
	// 	setParam(option.lbid)
	// 	//oppo
	// }
	
	if(option.requestId){
		console.log(option.requestId)
		let a={
			'requestId':option.requestId,
			'adId':option.adId,
			'pn':option.pn
		}
		console.log(a)
		if(a.requestId==null||a.requestId==undefined||a.requestId=='null'){
			a.requestId=''
		}
		if(a.adId==null||a.adId==undefined||a.adId=='null'){
			a.adId=''
		}
		if(a.pn==null||a.pn==undefined||a.pn=='null'){
			a.pn=''
		}
		console.log(JSON.stringify(a))
		setParam(JSON.stringify(a))
		
	}
	if(option.projectid){
	
		setParam(window.location.href)
		
	}
	if(option.request){
	
		setParam(window.location.href)
		
	}
	
	if(option.creativeid){
	
	setParam(window.location.href)
		
	}
	if (option.union_site) {
		setParam(window.location.href)
	}
	
}

const setParam = (value) => {
	uni.setStorageSync('param', value)
}

const getParam = () => {
	console.log('getParam1', getPushId(), getParamToken())
	if (getPushId() && getParamToken()) {
		console.log('getParam2')
		return JSON.stringify({
			"push_id": getPushId(),
			"token": getParamToken()
		})
	}
	console.log('getParam3')
	return uni.getStorageSync('param')
}

const removeParam = () => {
	uni.removeStorageSync('param')
}

const setPushId = (value) => {
	uni.setStorageSync('paramPushId', value)
}

const getPushId = () => {
	return uni.getStorageSync('paramPushId')
}

const removePushId = () => {
	uni.removeStorageSync('paramPushId')
}

const setParamToken = (value) => {
	uni.setStorageSync('paramToken', value)
}

const getParamToken = () => {
	return uni.getStorageSync('paramToken')
}

const removeParamToken = () => {
	uni.removeStorageSync('paramToken')
}

const clearParam = () => {
	removeParam()
	removeParamToken()
	removePushId()
}

export default {
	initData,
	setParam,
	getParam,
	setParamToken,
	getParamToken,
	setPushId,
	getPushId,
	clearParam,
}